﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StudentManagement.Forms;
using StudentManagement.Utils;
using StudentManagement.Interfaces;

namespace StudentManagement
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            // Feature 
            FeatureControl.NewStudentEvent += new EventHandler(FeatureControl_NewStudentEvent);
            FeatureControl.ExitEvent += new EventHandler(FeatureControl_ExitEvent);
            FeatureControl.SaveFileEvent += new EventHandler<FileEventArgs>(FeatureControl_SaveLoadFileEvent);
            FeatureControl.LoadFileEvent += new EventHandler<FileEventArgs>(FeatureControl_LoadFileEvent);
            // Search
            SearchPanelControl.SearchClick += new EventHandler<FilterEventArgs>(SearchPanelControl_SearchClick);
            ShowListStudentOnGrid();
        }

        private void FeatureControl_LoadFileEvent(object sender, FileEventArgs e)
        {
            StudentManage.GetInstance().LoadFile(e.PathFile);
            ShowListStudentOnGrid();
        }

        private void FeatureControl_SaveLoadFileEvent(object sender, FileEventArgs e)
        {
            StudentManage.GetInstance().SaveFile(e.PathFile);
        }

        private void FeatureControl_ExitEvent(object sender, EventArgs e)
        {
            this.Close();
        }

        NewStudentForm newStudentForm;
        public void FeatureControl_NewStudentEvent(object sender, EventArgs e)
        {
            newStudentForm = new NewStudentForm();
            newStudentForm.CreateClick += new EventHandler<Utils.StudentEventArgs>(newStudentForm_CreateClick);
            newStudentForm.ShowDialog();
        }

        private void newStudentForm_CreateClick(object sender, Utils.StudentEventArgs e)
        {
            newStudentForm.Close();
            StudentManage.GetInstance().InsertNewStudent(e.Student);
            ShowListStudentOnGrid();
        }

        private void SearchPanelControl_SearchClick(object sender, FilterEventArgs e)
        {
            ISearchType searchType = SearchTypeFactory.GetInstance().GetSearchType(e.SearchType);
            
            dgListStudent.DataSource= searchType.Search(e.Filter);
        }

        private void ShowListStudentOnGrid()
        {
            dgListStudent.DataSource = StudentManage.GetInstance().InteriorListStudent();
        }
    }
}
